package com.lahuan.controller;


import com.lahuan.demo.entity.AccountUsd;
import com.lahuan.demo.service.AccountRmbService;
import com.lahuan.demo.service.AccountUsdService;
import com.lahuan.demo.service.FrozeAccountRmbService;
import com.lahuan.demo.service.FrozeAccountUsdService;
import com.lahuan.transaction.TestTransaction;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.apache.skywalking.apm.toolkit.trace.TraceContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.UUID;

/**
 * @author lfy
 */
@RestController
@RequestMapping
public class TestController {

    @Autowired
    TestTransaction testTransaction;
    @DubboReference(version = "1.0.0", interfaceClass = AccountUsdService.class)
    private AccountUsdService accountUsdService;
    @DubboReference(version = "1.0.0", interfaceClass = AccountRmbService.class)
    private AccountRmbService accountRmbService;

    @DubboReference(version = "1.0.0", interfaceClass = FrozeAccountRmbService.class)
    private FrozeAccountRmbService frozeAccountRmbService;
    @DubboReference(version = "1.0.0", interfaceClass = FrozeAccountUsdService.class)
    private FrozeAccountUsdService frozeAccountUsdService;

    @GetMapping("/trans")
    public Object trans() {
        try {
            testTransaction.tryTransaction(1L,2L,new BigDecimal(7),new BigDecimal(1), UUID.randomUUID().toString());
            return "success";
        }catch (Exception ex){
            return ex.getMessage();
        }
    }

    @GetMapping("/test")
    public Object test() {
        try {
            for (int i = 0; i < 5; i++) {
                accountRmbService.froze(1L,BigDecimal.ONE,"AA");;
            }
            return  accountRmbService.getById(1);
        }catch (Exception ex){
            ex.printStackTrace();
            return ex.getMessage();
        }
    }
    @Trace
    @GetMapping("/test2")
    public Object test2() {
        try {
            String traceId = TraceContext.traceId();
            System.out.println(traceId);
            accountUsdService.froze(1L,BigDecimal.ONE,"AA");;
            return  1;
        }catch (Exception ex){
            ex.printStackTrace();
            return ex.getMessage();
        }
    }
}
